using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using System.ComponentModel;
using System.Transactions;
using System.Windows.Forms;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

using EntLibContrib.PolicyInjection.CallHandlers;

namespace EntLibContrib.PolicyInjection.Configuration
{
    [Assembler(typeof(TransactionScopeCallHandlerAssembler))]
    public class TransactionScopeCallHandlerData : CallHandlerData
    {
        private const string CompletePropertyName = "complete";
        private const string TransactionScopeOptionPropertyName = "scopeOption";
        private const string TimeoutPropertyName = "timeout";
        private const string IsolationLevelPropertyName = "isolationLevel";
        private const string EnterpriseServicesInteropOptionPropertyName = "interopOption";

        /// <summary>
        /// Construct a new <see cref="TransactionScopeCallHandlerData"/>.
        /// </summary>
        public TransactionScopeCallHandlerData()
        {
        }

        /// <summary>
        /// Construct a new <see cref="TransactionScopeCallHandlerData"/>.
        /// </summary>
        /// <param name="handlerName">Name of the handler.</param>
        
        public TransactionScopeCallHandlerData(string handlerName)
            : base(handlerName, typeof(TransactionScopeCallHandler))
        {
        }

        /// <summary>
        /// Construct a new <see cref="TransactionScopeCallHandlerData"/>.
        /// </summary>
        /// <param name="handlerName">Name of the handler.</param>
        /// <param name="scopeOption">TransactionScopeOption to use in handler.</param>
        public TransactionScopeCallHandlerData(string handlerName, TransactionScopeOption scopeOption)
            : base(handlerName, typeof(TransactionScopeCallHandler))
        {
            ScopeOption = scopeOption;
        }

        /// <summary>
        /// Gets or Sets whether the TransactionScope will complete on success.
        /// </summary>
        /// <value>The "complete" attribute in configuration</value>
        [ConfigurationProperty(CompletePropertyName, DefaultValue = "true")]
        public bool Complete
        {
            get { return (bool)base[CompletePropertyName]; }
            set { base[CompletePropertyName] = value; }
        }

        /// <summary>
        /// Gets or Sets the Scope Option.
        /// </summary>
        /// <value>The "scopeOption" attribute in configuration</value>
        [ConfigurationProperty(TransactionScopeOptionPropertyName, DefaultValue = "Required")]
        public TransactionScopeOption ScopeOption
        {
            get { return (TransactionScopeOption)base[TransactionScopeOptionPropertyName]; }
            set { base[TransactionScopeOptionPropertyName] = value; }
        }

        /// <summary>
        /// Gets or Sets the Timeout.
        /// </summary>
        /// <value>The "timeout" attribute in configuration</value>
        [ConfigurationProperty(TimeoutPropertyName, DefaultValue = "00:01:00")]
        public TimeSpan Timeout
        {
            get { return (TimeSpan)base[TimeoutPropertyName]; }
            set { base[TimeoutPropertyName] = value; }
        }

        /// <summary>
        /// Gets or Sets the Isolation Level.
        /// </summary>
        /// <value>The "isolationLevel" attribute in configuration</value>
        [ConfigurationProperty(IsolationLevelPropertyName, DefaultValue = "Serializable")]
        public IsolationLevel IsolationLevel
        {
            get { return (IsolationLevel)base[IsolationLevelPropertyName]; }
            set { base[IsolationLevelPropertyName] = value; }
        }

        /// <summary>
        /// Gets or Sets the EnterpriseServicesInteropOption.
        /// </summary>
        /// <value>The "interopOption" attribute in configuration</value>
        [ConfigurationProperty(EnterpriseServicesInteropOptionPropertyName, DefaultValue = "None")]
        public EnterpriseServicesInteropOption InteropOption
        {
            get { return (EnterpriseServicesInteropOption)base[EnterpriseServicesInteropOptionPropertyName]; }
            set { base[EnterpriseServicesInteropOptionPropertyName] = value; }
        }
    }

    /// <summary>
    /// Class used by ObjectBuilder to construct an <see cref="TransactionScopeCallHandler"/> from
    /// a <see cref="TransactionScopeCallHandlerData"/> instance.
    /// </summary>
    public class TransactionScopeCallHandlerAssembler : IAssembler<ICallHandler, CallHandlerData>
    {
        /// <summary>
        /// Builds an instance of the subtype of <typeparamref name="TObject"/> type the receiver knows how to build,  based on 
        /// an a configuration object.
        /// </summary>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="objectConfiguration">The configuration object that describes the object to build.</param>
        /// <param name="configurationSource">The source for configuration objects.</param>
        /// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
        /// <returns>A fully initialized instance of the <typeparamref name="TObject"/> subtype.</returns>
        public ICallHandler Assemble(IBuilderContext context, CallHandlerData objectConfiguration,
                                     IConfigurationSource configurationSource,
                                     ConfigurationReflectionCache reflectionCache)
        {
            TransactionScopeCallHandlerData handlerData = (TransactionScopeCallHandlerData)objectConfiguration;

            TransactionOptions options = new TransactionOptions();
            options.IsolationLevel = handlerData.IsolationLevel;
            options.Timeout = handlerData.Timeout;

            TransactionScopeCallHandler handler = new TransactionScopeCallHandler(
                handlerData.ScopeOption, 
                options, 
                handlerData.InteropOption
                , handlerData.Complete);

            return handler;
        }
    }
}
